import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    user: null,
    isLoggedIn: false
  }),
  actions: {
    login(user) {
      this.user = user
      this.isLoggedIn = true
      localStorage.setItem('user', JSON.stringify(user))
    },
    logout() {
      this.user = null
      this.isLoggedIn = false
      localStorage.removeItem('user')
    },
    initUser() {
      const userStr = localStorage.getItem('user')
      if (userStr) {
        this.user = JSON.parse(userStr)
        this.isLoggedIn = true
      }
    }
  }
})

export const useProductsStore = defineStore('products', {
  state: () => ({
    products: []
  }),
  actions: {
    setProducts(products) {
      this.products = products
    }
  }
})

export default {
  useUserStore,
  useProductsStore
}

